home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / wemdemo4.zip / INFO / ELISP.4 < prev    next >
Text File  |  1994-09-21  |  50KB  |  1,359 lines

  1. Info file elisp, produced by Makeinfo, -*- Text -*- from input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual,   for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts
  10. Avenue,  Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of
  15. this manual provided the copyright notice and this permission notice
  16. are preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28.  
  29. 
  30. File: elisp,  Node: Character Case,  Prev: Formatting Strings,  Up: Strings and Characters
  31.  
  32. Character Case
  33. ==============
  34.  
  35.    The character case functions change the case of single characters
  36. or of the contents of strings.  The functions convert only alphabetic
  37. characters (the letters `A' through `Z' and `a' through `z'); other
  38. characters are not altered.  The functions do not modify the strings
  39. that are passed to them as arguments.
  40.  
  41.    The examples below use the characters `X' and `x' which have ASCII
  42. codes 88 and 120 respectively.
  43.  
  44.  * Function: downcase STRING-OR-CHAR
  45.      This function converts a character or a string to lower case.
  46.  
  47.      When the argument to `downcase' is a string, the function
  48.      creates and returns a new string in which each letter in the
  49.      argument that is upper case is converted to lower case.  When
  50.      the argument to `downcase' is a character, `downcase' returns
  51.      the corresponding lower case character.  This value is an
  52.      integer.  If the original character is lower case, or is not a
  53.      letter, then the value equals the original character.
  54.  
  55.           (downcase "The cat in the hat")
  56.                => "the cat in the hat"
  57.           
  58.           (downcase ?X)
  59.                => 120
  60.  
  61.  * Function: upcase STRING-OR-CHAR
  62.      This function converts a character or a string to upper case.
  63.  
  64.      When the argument to `upcase' is a string, the function creates
  65.      and returns a new string in which each letter in the argument
  66.      that is lower case is converted to upper case.
  67.  
  68.      When the argument to `upcase' is a character, `upcase' returns
  69.      the corresponding upper case character.  This value is an integer.
  70.      If the original character is upper case, or is not a letter,
  71.      then the value equals the original character.
  72.  
  73.           (upcase "The cat in the hat")
  74.                => "THE CAT IN THE HAT"
  75.           
  76.           (upcase ?x)
  77.                => 88
  78.  
  79.  * Function: capitalize STRING-OR-CHAR
  80.      This function capitalizes strings or characters.  If
  81.      STRING-OR-CHAR is a string, the function creates and returns a
  82.      new string, whose contents are a copy of STRING-OR-CHAR in which
  83.      each word has been capitalized.  This means that the first
  84.      character of each word is converted to upper case, and the rest
  85.      are converted to lower case.
  86.  
  87.      The definition of a word is any sequence of consecutive
  88.      characters that are assigned to the word constituent category in
  89.      the current syntax table (*Note Syntax Class Table::).
  90.  
  91.      When the argument to `capitalize' is a character, `capitalize'
  92.      has the same result as `upcase'.
  93.  
  94.           (capitalize "The cat in the hat")
  95.                => "The Cat In The Hat"
  96.           
  97.           (capitalize "THE 77TH-HATTED CAT")
  98.                => "The 77th-Hatted Cat"
  99.  
  100.              (capitalize ?x)
  101.                => 88
  102.  
  103.  
  104. 
  105. File: elisp,  Node: Lists,  Next: Sequences Arrays Vectors,  Prev: Strings and Characters,  Up: Top
  106.  
  107. Lists
  108. *****
  109.  
  110.    A "list" represents a sequence of zero or more elements (which may
  111. be any Lisp objects).  The important difference between lists and
  112. vectors is that two or more lists can share part of their structure;
  113. in addition, you can insert or delete elements in a list without
  114. copying the whole list.
  115.  
  116. * Menu:
  117.  
  118. * Cons Cells::          How lists are made out of cons cells.
  119. * Lists as Boxes::                 Graphical notation to explain lists.
  120. * List-related Predicates::        Is this object a list?  Comparing two lists.
  121. * List Elements::       Extracting the pieces of a list.
  122. * Building Lists::      Creating list structure.
  123. * Modifying Lists::     Storing new pieces into an existing list.
  124. * Sets And Lists::      A list can represent a finite mathematical set.
  125. * Association Lists::   A list can represent a finite relation or mapping.
  126.  
  127.  
  128. 
  129. File: elisp,  Node: Cons Cells,  Next: Lists as Boxes,  Prev: Lists,  Up: Lists
  130.  
  131. Lists and Cons Cells
  132. ====================
  133.  
  134.    Lists in Lisp are not a primitive data type; they are built up
  135. from "cons cells".  A cons cell is a data object which represents an
  136. ordered pair.  It records two Lisp objects, one labeled as the CAR,
  137. and the other labeled as the CDR.  (These names are traditional.)
  138.  
  139.    A list is made by chaining cons cells together, one cons cell per
  140. element.  By convention, the CARs of the cons cells are the elements
  141. of the list, and the CDRs are used to chain the list: the CDR of each
  142. cons cell is the following cons cell.  The CDR of the last cons cell
  143. is `nil'.  This asymmetry between the CAR and the CDR is entirely a
  144. matter of convention; at the level of cons cells, the CAR and CDR
  145. slots have the same characteristics.
  146.  
  147.    The symbol `nil' is considered a list as well as a symbol; it is
  148. the list with no elements.  Therefore, the CDR of any nonempty list L
  149. is a list containing all the elements of L except the first.  For
  150. convenience, the symbol `nil' is considered to have `nil' as its CDR
  151. (and also as its CAR).
  152.  
  153.  
  154. 
  155. File: elisp,  Node: Lists as Boxes,  Next: List-related Predicates,  Prev: Cons Cells,  Up: Lists
  156.  
  157. Lists as Linked Pairs of Boxes
  158. ==============================
  159.  
  160.    A cons cell can be illustrated as a pair of boxes.  The first box
  161. represents the CAR and the second box represents the CDR.  Here is an
  162. illustration of the two-element list, `(tulip lily)', made from two
  163. cons cells:
  164.  
  165.       ---------------         ---------------
  166.      |car    |cdr    |       |car    |cdr    |
  167.      | tulip |   o---------->| lily  |  nil  |
  168.      |       |       |       |       |       |
  169.       ---------------         ---------------
  170.  
  171.    Each pair of boxes represents a cons cell.  Each box "refers to",
  172. "points to" or "contains" a Lisp object.  (These terms are
  173. synonymous.)  The first box, which is the CAR of the first cons cell,
  174. contains the symbol `tulip'.  The arrow from the CDR of the first
  175. cons cell to the second cons cell indicates that the CDR of the first
  176. cons cell points to the second cons cell.
  177.  
  178.    The same list can be illustrated in a different sort of box
  179. notation like this:
  180.  
  181.          ___ ___      ___ ___
  182.         |___|___|--> |___|___|--> nil
  183.           |            |
  184.           |            |
  185.            --> tulip    --> lily
  186.  
  187.    Here is a more complex illustration, this time of the
  188. three-element list, `((pine needles) oak maple)', the first element
  189. of which is a two-element list:
  190.  
  191.          ___ ___      ___ ___      ___ ___
  192.         |___|___|--> |___|___|--> |___|___|--> nil
  193.           |            |            |
  194.           |            |            |
  195.           |             --> oak      --> maple
  196.           |
  197.           |     ___ ___      ___ ___
  198.            --> |___|___|--> |___|___|--> nil
  199.                  |            |
  200.                  |            |
  201.                   --> pine     --> needles
  202.  
  203.    The same list is represented in the first box notation like this:
  204.  
  205.       ---------------         ---------------         ---------------
  206.      |car    |cdr    |       |car    |cdr    |       |car    |cdr    |
  207.      |   o   |   o---------->|  oak  |   o---------->| maple |  nil  |
  208.      |   |   |       |       |       |       |       |       |       |
  209.       -- | ----------         ---------------         ---------------
  210.          |
  211.          |
  212.          |        ---------------         -----------------
  213.          |       |car    |cdr    |       |car      |cdr    |
  214.           ------>| pine  |   o---------->| needles |  nil  |
  215.                  |       |       |       |         |       |
  216.                   ---------------         -----------------
  217.  
  218.    *Note List Type::, for the read and print syntax of lists, and for
  219. more "box and arrow" illustrations of lists.
  220.  
  221.  
  222. 
  223. File: elisp,  Node: List-related Predicates,  Next: List Elements,  Prev: Lists as Boxes,  Up: Lists
  224.  
  225. Predicates on Lists
  226. ===================
  227.  
  228.    The following predicates test whether a Lisp object is an atom, is
  229. a cons cell or is a list, or whether it is the distinguished object
  230. `nil'.  (Many of these tests can be defined in terms of the others,
  231. but they are used so often that it is worth having all of them.)
  232.  
  233.  * Function: consp OBJECT
  234.      This function returns `t' if OBJECT is a cons cell, `nil'
  235.      otherwise.  `nil' is not a cons cell, although it *is* a list.
  236.  
  237.  * Function: atom OBJECT
  238.      This function returns `t' if OBJECT is an atom, `nil' otherwise.
  239.      All objects except cons cells are atoms.  The symbol `nil' is an
  240.      atom and is also a list; it is the only Lisp object which is both.
  241.  
  242.           (atom OBJECT) == (not (consp OBJECT))
  243.  
  244.  * Function: listp OBJECT
  245.      This function returns `t' if OBJECT is a cons cell or `nil'. 
  246.      Otherwise, it returns `nil'.
  247.  
  248.           (listp '(1))
  249.                => t
  250.           (listp '())
  251.                => t
  252.  
  253.  * Function: nlistp OBJECT
  254.      This function is the opposite of `listp': it returns `t' if
  255.      OBJECT is not a list.  Otherwise, it returns `nil'.
  256.  
  257.           (listp OBJECT) == (not (nlistp OBJECT))
  258.  
  259.  * Function: null OBJECT
  260.      This function returns `t' if OBJECT is `nil', and returns `nil'
  261.      otherwise.  This function is identical to `not', but as a matter
  262.      of clarity we use `null' when OBJECT is considered a list and
  263.      `not' when it is considered a truth value (see `not' in *Note
  264.      Combining Conditions::).
  265.  
  266.           (null '(1))
  267.                => nil
  268.           (null '())
  269.                => t
  270.  
  271.  
  272. 
  273. File: elisp,  Node: List Elements,  Next: Building Lists,  Prev: List-related Predicates,  Up: Lists
  274.  
  275. Accessing Elements of Lists
  276. ===========================
  277.  
  278.  * Function: car CONS-CELL
  279.      This function returns the value pointed to by the first pointer
  280.      of the cons cell CONS-CELL.  Expressed another way, this
  281.      function returns the CAR of CONS-CELL.
  282.  
  283.      As a special case, if CONS-CELL is `nil', then `car' is defined
  284.      to return `nil'; therefore, any list is a valid argument for
  285.      `car'.  An error is signaled if the argument is not a cons cell
  286.      or `nil'.
  287.  
  288.           (car '(a b c))
  289.                => a
  290.           (car '())
  291.                => nil
  292.  
  293.  * Function: cdr CONS-CELL
  294.      This function returns the value pointed to by the second pointer
  295.      of the cons cell CONS-CELL.  Expressed another way, this
  296.      function returns the CDR of CONS-CELL.
  297.  
  298.      As a special case, if CONS-CELL is `nil', then `cdr' is defined
  299.      to return `nil'; therefore, any list is a valid argument for
  300.      `cdr'.  An error is signaled if the argument is not a cons cell
  301.      or `nil'.
  302.  
  303.           (cdr '(a b c))
  304.                => (b c)
  305.           (cdr '())
  306.                => nil
  307.  
  308.  * Function: car-safe OBJECT
  309.      This function lets you take the CAR of a cons cell while
  310.      avoiding errors for other data types.  It returns the CAR of
  311.      OBJECT if OBJECT is a cons cell, `nil' otherwise.  This is in
  312.      contrast to `car', which signals an error if OBJECT is not a list.
  313.  
  314.           (car-safe OBJECT)
  315.           ==
  316.           (let ((x OBJECT))
  317.             (if (consp x)
  318.                 (car x)
  319.               nil))
  320.  
  321.  * Function: cdr-safe OBJECT
  322.      This function lets you take the CDR of a cons cell while
  323.      avoiding errors for other data types.  It returns the CDR of
  324.      OBJECT if OBJECT is a cons cell, `nil' otherwise.  This is in
  325.      contrast to `cdr', which signals an error if OBJECT is not a list.
  326.  
  327.           (cdr-safe OBJECT)
  328.           ==
  329.           (let ((x OBJECT))
  330.             (if (consp x)
  331.                 (cdr x)
  332.               nil))
  333.  
  334.  * Function: nth N LIST
  335.      This function returns the Nth element of LIST.  Elements are
  336.      numbered starting with zero, so the CAR of LIST is element
  337.      number zero.  If the length of LIST is N or less, the value is
  338.      `nil'.
  339.  
  340.      If N is less than zero, then the first element is returned.
  341.  
  342.           (nth 2 '(1 2 3 4))
  343.                => 3
  344.           (nth 10 '(1 2 3 4))
  345.                => nil
  346.           (nth -3 '(1 2 3 4))
  347.                => 1
  348.           
  349.           (nth n x) == (car (nthcdr n x))
  350.  
  351.  * Function: nthcdr N LIST
  352.      This function returns the Nth cdr of LIST.  In other words, it
  353.      removes the first N links of LIST and returns what follows.
  354.  
  355.      If N is less than or equal to zero, then all of LIST is
  356.      returned.  If the length of LIST is N or less, the value is `nil'.
  357.  
  358.           (nthcdr 1 '(1 2 3 4))
  359.                => (2 3 4)
  360.           (nthcdr 10 '(1 2 3 4))
  361.                => nil
  362.           (nthcdr -3 '(1 2 3 4))
  363.                => (1 2 3 4)
  364.  
  365.  
  366. 
  367. File: elisp,  Node: Building Lists,  Next: Modifying Lists,  Prev: List Elements,  Up: Lists
  368.  
  369. Building Cons Cells and Lists
  370. =============================
  371.  
  372.    Many functions build lists, as lists reside at the very heart of
  373. Lisp.  `cons' is the fundamental list-building function; however, it
  374. is interesting to note that `list' is used more times in the source
  375. code for Emacs than `cons'.
  376.  
  377.  * Function: cons OBJECT1 OBJECT2
  378.      This function is the fundamental function used to build new list
  379.      structure.  It creates a new cons cell, making OBJECT1 the CAR,
  380.      and OBJECT2 the CDR.  It then returns the new cons cell.  The
  381.      arguments OBJECT1 and OBJECT2 may be any Lisp objects, but most
  382.      often OBJECT2 is a list.
  383.  
  384.           (cons 1 '(2))
  385.                => (1 2)
  386.           (cons 1 '())
  387.                => (1)
  388.           (cons 1 2)
  389.                => (1 . 2)
  390.  
  391.      `cons' is often used to add a single element to the front of a
  392.      list.  This is called "consing the element onto the list".  For
  393.      example:
  394.  
  395.           (setq list (cons newelt list))
  396.  
  397.  * Function: list &rest OBJECTS
  398.      This function creates a list with OBJECTS as its elements.  The
  399.      resulting list is always `nil'-terminated.  If no OBJECTS are
  400.      given, the empty list is returned.
  401.  
  402.           (list 1 2 3 4 5)
  403.                => (1 2 3 4 5)
  404.           (list 1 2 '(3 4 5) 'foo)
  405.                => (1 2 (3 4 5) foo)
  406.  
  407.              (list)
  408.                => nil
  409.  
  410.  * Function: make-list LENGTH OBJECT
  411.      This function creates a list of length LENGTH, in which all the
  412.      elements have the identical value OBJECT.  Compare `make-list'
  413.      with `make-string' (*note Creating Strings::.).
  414.  
  415.           (make-list 3 'pigs)
  416.                => (pigs pigs pigs)
  417.           (make-list 0 'pigs)
  418.                => nil
  419.  
  420.  * Function: append &rest SEQUENCES
  421.      This function returns a list containing all the elements of
  422.      SEQUENCES.  The SEQUENCES may be lists, vectors, strings, or
  423.      integers.  All arguments except the last one are copied, so none
  424.      of them are altered.
  425.  
  426.      The final argument to `append' may be any object but it is
  427.      typically a list.  The final argument is not copied or
  428.      converted; it becomes part of the structure of the new list.
  429.  
  430.      Here is an example:
  431.  
  432.           (setq trees '(pine oak))
  433.                => (pine oak)
  434.           (setq more-trees (append '(maple birch) trees))
  435.                => (maple birch pine oak)
  436.           
  437.           trees
  438.                => (pine oak)
  439.           more-trees
  440.                => (maple birch pine oak)
  441.           (eq trees (cdr (cdr more-trees)))
  442.                => t
  443.  
  444.      You can see what happens by looking at a box diagram.  The
  445.      variable `trees' is set to the list `(pine oak)' and then the
  446.      variable `more-trees' is set to the list `(maple birch pine oak)'.
  447.      However, the variable `trees' continues to refer to the original
  448.      list:
  449.  
  450.           more-trees                trees
  451.           |                           |
  452.           |     ___ ___      ___ ___   -> ___ ___      ___ ___
  453.            --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil
  454.                  |            |            |            |
  455.                  |            |            |            |
  456.                   --> maple    -->birch     --> pine     --> oak
  457.  
  458.      An empty sequence contributes nothing to the value returned by
  459.      `append'.  As a consequence of this, a final `nil' argument
  460.      forces a copy of the previous argument.
  461.  
  462.           trees
  463.                => (pine oak)
  464.           (setq wood (append trees ()))
  465.                => (pine oak)
  466.           wood
  467.                => (pine oak)
  468.           (eq wood trees)
  469.                => nil
  470.  
  471.      This once was the standard way to copy a list, before the
  472.      function `copy-sequence' was invented.  *Note Sequences Arrays
  473.      Vectors::.
  474.  
  475.      With the help of `apply', we can append all the lists in a list
  476.      of lists:
  477.  
  478.           (apply 'append '((a b c) nil (x y z) nil))
  479.                => (a b c x y z)
  480.  
  481.      If no SEQUENCES are given, `nil' is returned:
  482.  
  483.           (append)
  484.                => nil
  485.  
  486.      In the special case where one of the SEQUENCES is an integer
  487.      (not a sequence of integers), it is first converted to a string
  488.      of digits making up the decimal print representation of the
  489.      integer.  This special case exists for compatibility with
  490.      Mocklisp, and we don't recommend you take advantage of it.  If
  491.      you want to convert an integer in this way, use `format' (*note
  492.      Formatting Strings::.) or `int-to-string' (*note String
  493.      Conversion::.).
  494.  
  495.           (setq trees '(pine oak))
  496.                => (pine oak)
  497.           (char-to-string ?\054)
  498.                => "6"
  499.           (setq longer-list (append trees 6 '(spruce)))
  500.                => (pine oak 54 spruce)
  501.           (setq x-list (append trees 6 6))
  502.                => (pine oak 54 . 6)
  503.  
  504.      See `nconc' in *Note Rearrangement::, for another way to join
  505.      lists without copying.
  506.  
  507.  * Function: reverse LIST
  508.      This function creates a new list whose elements are the elements
  509.      of LIST, but in reverse order.  The original argument LIST is
  510.      *not* altered.
  511.  
  512.           (setq x '(1 2 3 4))
  513.                => (1 2 3 4)
  514.           (reverse x)
  515.                => (4 3 2 1)
  516.           x
  517.                => (1 2 3 4)
  518.  
  519.  
  520. 
  521. File: elisp,  Node: Modifying Lists,  Next: Sets And Lists,  Prev: Building Lists,  Up: Lists
  522.  
  523. Modifying Existing List Structure
  524. =================================
  525.  
  526.    You can modify the CAR and CDR contents of a cons cell with the
  527. primitives `setcar' and `setcdr'.
  528.  
  529.      Common Lisp note: Common Lisp uses functions `rplaca' and
  530.      `rplacd' to alter list structure; they change structure the same
  531.      way as `setcar' and `setcdr', but the Common Lisp functions
  532.      return the cons cell while `setcar' and `setcdr' return the new
  533.      CAR or CDR.
  534.  
  535. * Menu:
  536.  
  537. * Setcar::          Replacing an element in a list.
  538. * Setcdr::          Replacing part of the list backbone.
  539.                       This can be used to remove or add elements.
  540. * Rearrangement::   Reordering the elements in a list; combining lists.
  541.  
  542.  
  543. 
  544. File: elisp,  Node: Setcar,  Next: Setcdr,  Prev: Modifying Lists,  Up: Modifying Lists
  545.  
  546. Altering List Elements with `setcar'
  547. ------------------------------------
  548.  
  549.    Changing the CAR of a cons cell is done with `setcar' and replaces
  550. one element of a list with a different element.
  551.  
  552.  * Function: setcar CONS OBJECT
  553.      This function stores OBJECT as the new CAR of CONS, replacing
  554.      its previous CAR.  It returns the value OBJECT.  For example:
  555.  
  556.           (setq x '(1 2))
  557.                => (1 2)
  558.           (setcar x '4)
  559.                => 4
  560.           x
  561.                => (4 2)
  562.  
  563.    When a cons cell is part of the shared structure of several lists,
  564. storing a new CAR into the cons changes one element of each of these
  565. lists.  Here is an example:
  566.  
  567.      ;; Create two lists that are partly shared.
  568.      (setq x1 '(a b c))
  569.           => (a b c)
  570.      (setq x2 (cons 'z (cdr x1)))
  571.           => (z b c)
  572.      
  573.      ;; Replace the CAR of a shared link.
  574.      (setcar (cdr x1) 'foo)
  575.           => foo
  576.      x1                           ; Both lists are changed.
  577.           => (a foo c)
  578.      x2
  579.           => (z foo c)
  580.      
  581.      ;; Replace the CAR of a link that is not shared.
  582.      (setcar x1 'baz)
  583.           => baz
  584.      x1                           ; Only one list is changed.
  585.           => (baz foo c)
  586.      x2
  587.           => (z foo c)
  588.  
  589.    Here is a graphical depiction of the shared structure of the two
  590. lists X1 and X2, showing why replacing `b' changes them both:
  591.  
  592.              ___ ___        ___ ___      ___ ___
  593.      x1---> |___|___|----> |___|___|--> |___|___|--> nil
  594.               |        -->   |            |
  595.               |       |      |            |
  596.                --> a  |       --> b        --> c
  597.                       |
  598.             ___ ___   |
  599.      x2--> |___|___|--
  600.              |
  601.              |
  602.               --> z
  603.  
  604.    Here is an alternative form of box diagram, showing the same
  605. relationship:
  606.  
  607.      x1:
  608.       ---------------         ---------------         ---------------
  609.      |car    |cdr    |       |car    |cdr    |       |car    |cdr    |
  610.      |   a   |   o---------->|   b   |   o---------->|   c   |  nil  |
  611.      |       |       |    -->|       |       |       |       |       |
  612.       ---------------    |    ---------------         ---------------
  613.                          |
  614.      x2:                 |
  615.       ---------------    |
  616.      |car    |cdr    |   |
  617.      |   z   |   o-------
  618.      |       |       |
  619.       ---------------
  620.  
  621.  
  622. 
  623. File: elisp,  Node: Setcdr,  Next: Rearrangement,  Prev: Setcar,  Up: Modifying Lists
  624.  
  625. Altering the CDR of a List
  626. --------------------------
  627.  
  628.    The lowest-level primitive for modifying a CDR is `setcdr':
  629.  
  630.  * Function: setcdr CONS OBJECT
  631.      This function stores OBJECT into the cdr of CONS.  The value
  632.      returned is OBJECT, not CONS.
  633.  
  634.    Here is an example of replacing the CDR of a list with a different
  635. list.  All but the first element of the list are removed in favor of
  636. a different sequence of elements.  The first element is unchanged,
  637. because it resides in the CAR of the list, and is not reached via the
  638. CDR.
  639.  
  640.      (setq x '(1 2 3))
  641.           => (1 2 3)
  642.      (setcdr x '(4))
  643.           => (4)
  644.  
  645.         x
  646.           => (1 4)
  647.  
  648.    You can delete elements from the middle of a list by altering the
  649. CDRs of the cons cells in the list.  For example, here we delete the
  650. second element, `b', from the list `(a b c)', by changing the CDR of
  651. the first cell:
  652.  
  653.      (setq x1 '(a b c))
  654.           => (a b c)
  655.      (setcdr x1 '(c))
  656.           => (c)
  657.      x1
  658.           => (a c)
  659.  
  660.    Here is the result in box notation:
  661.  
  662.                           -----------------------
  663.                          |                       |
  664.       ---------------    |    ---------------    |    ---------------
  665.      |car    |cdr    |   |   |car    |cdr    |    -->|car    |cdr    |
  666.      |   a   |   o-------    |   b   |   o---------->|   c   |  nil  |
  667.      |       |       |       |       |       |       |       |       |
  668.       ---------------         ---------------         ---------------
  669.  
  670. The second cons cell, which previously held the element `b', still
  671. exists and its CAR is still `b', but it no longer forms part of this
  672. list.
  673.  
  674.    It is equally easy to insert a new element by changing CDRs:
  675.  
  676.      (setq x1 '(a b c))
  677.           => (a b c)
  678.      (setcdr x1 (cons 'd (cdr x1)))
  679.           => (d b c)
  680.      x1
  681.           => (a d b c)
  682.  
  683.    Here is this result in box notation:
  684.  
  685.       ---------------         ---------------         ---------------
  686.      |car    |cdr    |       |car    |cdr    |       |car    |cdr    |
  687.      |   a   |   o   |    -->|   b   |   o---------->|   c   |  nil  |
  688.      |       |   |   |   |   |       |       |       |       |       |
  689.       ---------- | --    |    ---------------         ---------------
  690.                  |       |
  691.            ------         -------
  692.           |                      |
  693.           |    ---------------   |
  694.           |   |car    |cdr    |  |
  695.            -->|   d   |   o------
  696.               |       |       |
  697.                ---------------
  698.  
  699.  
  700. 
  701. File: elisp,  Node: Rearrangement,  Prev: Setcdr,  Up: Modifying Lists
  702.  
  703. Functions that Rearrange Lists
  704. ------------------------------
  705.  
  706.    Here are some functions that rearrange lists "destructively" by
  707. modifying the CDRs of their component cons cells.  We call these
  708. functions "destructive" because the original lists passed as
  709. arguments to them are chewed up to produce a new list that is
  710. subsequently returned.
  711.  
  712.  * Function: nconc &rest LISTS
  713.      This function returns a list containing all the elements of LISTS.
  714.      Unlike `append' (*note Building Lists::.), the LISTS are *not*
  715.      copied.  Instead, the last CDR of each of the LISTS is changed
  716.      to refer to the following list.  The last of the LISTS is not
  717.      altered.  For example:
  718.  
  719.           (setq x '(1 2 3))
  720.                => (1 2 3)
  721.           (nconc x '(4 5))
  722.                => (1 2 3 4 5)
  723.           x
  724.                => (1 2 3 4 5)
  725.  
  726.      Since the last argument of `nconc' is not itself modified, it is
  727.      reasonable to use a constant list, such as `'(4 5)', as is done
  728.      in the above example.  For the same reason, the last argument
  729.      need not be a list:
  730.  
  731.           (setq x '(1 2 3))
  732.                => (1 2 3)
  733.           (nconc x 'z)
  734.                => (1 2 3 . z)
  735.           x
  736.                => (1 2 3 . z)
  737.  
  738.      A common pitfall is to use a quoted constant list as a non-last
  739.      argument to `nconc'.  If you do this, your program will change
  740.      each time you run it!  Here is what happens:
  741.  
  742.           (defun add-foo (x)                ; This function should add
  743.             (nconc '(foo) x))               ; `foo' to the front of its arg.
  744.           
  745.           (symbol-function 'add-foo)
  746.                => (lambda (x) (nconc (quote (foo)) x))
  747.           
  748.           (setq xx (add-foo '(1 2)))        ; It seems to work.
  749.                => (foo 1 2)
  750.           (setq xy (add-foo '(3 4)))        ; What happened?
  751.                => (foo 1 2 3 4)
  752.           (eq xx xy)
  753.                => t
  754.           
  755.           (symbol-function 'add-foo)
  756.                => (lambda (x) (nconc (quote (foo 1 2 3 4) x)))
  757.  
  758.  * Function: nreverse LIST
  759.      This function reverses the order of the elements of LIST. 
  760.      Unlike `reverse', `nreverse' alters its argument destructively
  761.      by reversing the CDRs in the cons cells forming the list.  The
  762.      cons cell which used to be the last one in LIST becomes the
  763.      first cell of the value.
  764.  
  765.      For example:
  766.  
  767.           (setq x '(1 2 3 4))
  768.                => (1 2 3 4)
  769.           x
  770.                => (1 2 3 4)
  771.           (nreverse x)
  772.                => (4 3 2 1)
  773.           ;; The cell that was first is now last.
  774.           x
  775.                => (1)
  776.  
  777.      To avoid confusion, we usually store the result of `nreverse'
  778.      back in the same variable which held the original list:
  779.  
  780.           (setq x (nreverse x))
  781.  
  782.      Here is the `nreverse' of our favorite example, `(a b c)',
  783.      presented graphically:
  784.  
  785.           Original list head:                                 Reversed list:
  786.            ---------------         ---------------         ---------------
  787.           |car    |cdr    |       |car    |cdr    |       |car    |cdr    |
  788.           |   a   |  nil  |<--    |   b   |   o   |<--    |   c   |   o   |
  789.           |       |       |   |   |       |   |   |   |   |       |   |   |
  790.            ---------------    |    ---------- | --    |    ---------- | --
  791.                               |               |       |               |
  792.                                ---------------         ---------------
  793.  
  794.  * Function: sort LIST PREDICATE
  795.      This function sorts LIST stably, though destructively, and
  796.      returns the sorted list.  It compares elements using PREDICATE. 
  797.      A stable sort is one in which elements with equal sort keys
  798.      maintain their relative order before and after the sort. 
  799.      Stability is important when successive sorts are used to order
  800.      elements according to different criteria.
  801.  
  802.      The argument PREDICATE must be a function that accepts two
  803.      arguments.  It is called with two elements of LIST.  To get an
  804.      increasing order sort, the PREDICATE should return `t' if the
  805.      first element is "less than" the second, or `nil' if not.
  806.  
  807.      The destructive aspect of `sort' is that it rearranges the cons
  808.      cells forming LIST by changing CDRs.  A nondestructive sort
  809.      function would create new cons cells to store the elements in
  810.      their sorted order.  If you wish to sort a list without
  811.      destroying the original, copy it first with `copy-sequence'.
  812.  
  813.      The CARs of the cons cells are not changed; the cons cell that
  814.      originally contained the element `a' in LIST still has `a' in
  815.      its CAR after sorting, but it now appears in a different
  816.      position in the list due to the change of CDRs.  For example:
  817.  
  818.           (setq nums '(1 3 2 6 5 4 0))
  819.                => (1 3 2 6 5 4 0)
  820.           (sort nums '<)
  821.                => (0 1 2 3 4 5 6)
  822.           nums
  823.                => (1 2 3 4 5 6)
  824.  
  825.      Note that the list in `nums' no longer contains 0; this is the
  826.      same cons cell that it was before, but it is no longer the first
  827.      one in the list.  Don't assume a variable that formerly held the
  828.      argument now holds the entire sorted list!  Instead, save the
  829.      result of `sort' and use that.  Most often we store the result
  830.      back into the variable that held the original list:
  831.  
  832.           (setq nums (sort nums '<))
  833.  
  834.      *Note Sorting::, for more functions that perform sorting.  See
  835.      `documentation' in *Note Accessing Documentation::, for a useful
  836.      example of `sort'.
  837.  
  838.    See `delq', in *Note Sets And Lists::, for another function that
  839. modifies cons cells.
  840.  
  841.  
  842. 
  843. File: elisp,  Node: Sets And Lists,  Next: Association Lists,  Prev: Modifying Lists,  Up: Lists
  844.  
  845. Using Lists as Sets
  846. ===================
  847.  
  848.    A list can represent an unordered mathematical set--simply
  849. consider a value an element of a set if it appears in the list, and
  850. ignore the order of the list.  To form the union of two sets, use
  851. `append' (as long as you don't mind having duplicate elements).  Two
  852. other useful functions for sets are `memq' and `delq'.
  853.  
  854.      Common Lisp note: Common Lisp has functions `union' (which
  855.      avoids duplicate elements) and `intersection' for set
  856.      operations, but GNU Emacs Lisp does not have them.  You can
  857.      write them in Lisp if you wish.
  858.  
  859.  * Function: memq OBJECT LIST
  860.      This function tests to see whether OBJECT is a member of LIST. 
  861.      If it is, `memq' returns a list starting with the first
  862.      occurrence of OBJECT.  Otherwise, it returns `nil'.  The letter
  863.      `q' in `memq' says that it uses `eq' to compare OBJECT against
  864.      the elements of the list.  For example:
  865.  
  866.           (memq 2 '(1 2 3 2 1))
  867.                => (2 3 2 1)
  868.           (memq '(2) '((1) (2)))   ; `(2)' and `(2)' are not `eq'.
  869.                => nil
  870.  
  871.  * Function: delq OBJECT LIST
  872.      This function removes all elements `eq' to OBJECT from LIST.
  873.  
  874.      Elements at the front of the list are removed (when necessary)
  875.      simply by advancing down the list and returning a sublist that
  876.      starts after those elements:
  877.  
  878.           (delq 'a '(a b c))
  879.           ==
  880.           (cdr '(a b c))
  881.  
  882.      When an element to be deleted appears in the middle of the list,
  883.      removing it involves changing the CDRs (*note Setcdr::.).
  884.  
  885.           (setq sample-list '(1 2 3 (4)))
  886.                => (1 2 3 (4))
  887.           (delq 1 sample-list)
  888.                => (2 3 (4))
  889.           sample-list
  890.                => (1 2 3 (4))
  891.           (delq 2 sample-list)
  892.                => (1 3 (4))
  893.           sample-list
  894.                => (1 3 (4))
  895.  
  896.      Note that `(delq 2 sample-list)' removes the second element of
  897.      `sample-list', but `(delq 1 sample-list)' does not remove the
  898.      first element--it just returns a shorter list.  Don't assume
  899.      that a variable which formerly held the argument LIST now has
  900.      fewer elements, or that it still holds the original list! 
  901.      Instead, save the result of `delq' and use that.  Most often we
  902.      store the result back into the variable that held the original
  903.      list:
  904.  
  905.           (setq flowers (delq 'rose flowers))
  906.  
  907.      In the following example, the `(4)' that `delq' attempts to
  908.      match and the `(4)' in the `sample-list' are not `eq':
  909.  
  910.           (delq '(4) sample-list)
  911.                => (1 3 (4))
  912.  
  913.  
  914. 
  915. File: elisp,  Node: Association Lists,  Prev: Sets And Lists,  Up: Lists
  916.  
  917. Association Lists
  918. =================
  919.  
  920.    An "association list", or "alist" for short, records a mapping
  921. from keys to values.  It is a list of cons cells called
  922. "associations": the CAR of each cell is the "key", and the CDR is the
  923. "associated value".  (This usage of "key" is not related to the term
  924. "key sequence"; it means any object which can be looked up in a table.)
  925.  
  926.    Here is an example of an alist.  The key `pine' is associated with
  927. the value `cones'; the key `oak' is associated with `acorns'; and the
  928. key `maple' is associated with `seeds'.
  929.  
  930.      '((pine . cones)
  931.        (oak . acorns)
  932.        (maple . seeds))
  933.  
  934.    The associated values in an alist may be any Lisp objects; so may
  935. the keys.  For example, in the following alist, the symbol `a' is
  936. associated with the number `1', and the string `"b"' is associated
  937. with the *list* `(2 3)', which is the CDR of the alist element:
  938.  
  939.      ((a . 1) ("b" 2 3))
  940.  
  941.    Sometimes it is better to design an alist to store the associated
  942. value in the CAR of the CDR of the element.  Here is an example:
  943.  
  944.      '((rose red) (lily white) (buttercup yellow)))
  945.  
  946. Here we regard `red' as the value associated with `rose'.  One
  947. advantage of this method is that you can store other related
  948. information--even a list of other items--in the CDR of the CDR.  One
  949. disadvantage is that you cannot use `rassq' (see below) to find the
  950. element containing a given value.  When neither of these
  951. considerations is important, the choice is a matter of taste, as long
  952. as you are consistent about it for any given alist.
  953.  
  954.    Note that the same alist shown above could be regarded as having
  955. the associated value in the CDR of the element; the the value
  956. associated with `rose' would be the list `(red)'.
  957.  
  958.    Association lists are often used to record information that you
  959. might otherwise keep on a stack, since new associations may be added
  960. easily to the front of the list.  When searching an association list
  961. for an association with a given key, the first one found is returned,
  962. if there is more than one.
  963.  
  964.    In Emacs Lisp, it is *not* an error if an element of an
  965. association list is not a cons cell.  The alist search functions
  966. simply ignore such elements.  Many other versions of Lisp signal
  967. errors in such cases.
  968.  
  969.    Note that property lists are similar to association lists in
  970. several respects.  A property list behaves like an association list
  971. in which each key can occur only once.  *Note Property Lists::, for a
  972. comparison of property lists and association lists.
  973.  
  974.  * Function: assoc KEY ALIST
  975.      This function returns the first association for KEY in ALIST. 
  976.      It compares KEY against the alist elements using `equal' (*note
  977.      Equality Predicates::.).  It returns `nil' if no association in
  978.      ALIST has a CAR `equal' to KEY.  For example:
  979.  
  980.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  981.                => ((pine . cones) (oak . acorns) (maple . seeds))
  982.           (assoc 'oak trees)
  983.                => (oak . acorns)
  984.           (cdr (assoc 'oak trees))
  985.                => acorns
  986.           (assoc 'birch trees)
  987.                => nil
  988.  
  989.      Here is another example in which the keys and values are not
  990.      symbols:
  991.  
  992.           (setq needles-per-cluster
  993.                 '((2 . ("Austrian Pine" "Red Pine"))
  994.                   (3 . "Pitch Pine")
  995.                   (5 . "White Pine")))
  996.           
  997.           (cdr (assoc 3 needles-per-cluster))
  998.                => "Pitch Pine"
  999.           (cdr (assoc 2 needles-per-cluster))
  1000.                => ("Austrian Pine" "Red Pine")
  1001.  
  1002.  * Function: assq KEY ALIST
  1003.      This function is like `assoc' in that it returns the first
  1004.      association for KEY in ALIST, but it makes the comparison using
  1005.      `eq' instead of `equal'.  `assq' returns `nil' if no association
  1006.      in ALIST has a CAR `eq' to KEY.  This function is used more
  1007.      often than `assoc', since `eq' is faster than `equal' and most
  1008.      alists use symbols as keys.  *Note Equality Predicates::.
  1009.  
  1010.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1011.           
  1012.           (assq 'pine trees)
  1013.                => (pine . cones)
  1014.  
  1015.      On the other hand, `assq' is not usually useful in alists where
  1016.      the keys may not be symbols:
  1017.  
  1018.           (setq leaves
  1019.                 '(("simple leaves" . oak)
  1020.                   ("compound leaves" . horsechestnut)))
  1021.           
  1022.           (assq "simple leaves" leaves)
  1023.                => nil
  1024.           (assoc "simple leaves" leaves)
  1025.                => ("simple leaves" . oak)
  1026.  
  1027.  * Function: rassq ALIST VALUE
  1028.      This function returns the first association with value VALUE in
  1029.      ALIST.  It returns `nil' if no association in ALIST has a CDR
  1030.      `eq' to VALUE.
  1031.  
  1032.      `rassq' is like `assq' except that the CDR of the ALIST
  1033.      associations is tested instead of the CAR.  You can think of
  1034.      this as "reverse `assq'", finding the key for a given value.
  1035.  
  1036.      For example:
  1037.  
  1038.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1039.           
  1040.           (rassq 'acorns trees)
  1041.                => (oak . acorns)
  1042.           (rassq 'spores trees)
  1043.                => nil
  1044.  
  1045.      Note that `rassq' cannot be used to search for a value stored in
  1046.      the CAR of the CDR of an element:
  1047.  
  1048.           (setq colors '((rose red) (lily white) (buttercup yellow)))
  1049.           
  1050.           (rassq 'white colors)
  1051.                => nil
  1052.  
  1053.      In this case, the CDR of the association `(lily white)' is not
  1054.      the symbol `white', but rather the list `(white)'.  This can be
  1055.      seen more clearly if the association is written in dotted pair
  1056.      notation:
  1057.  
  1058.           (lily white) == (lily . (white))
  1059.  
  1060.  * Function: copy-alist ALIST
  1061.      This function returns a two-level deep copy of ALIST: it creates
  1062.      a new copy of each association, so that you can alter the
  1063.      associations of the new alist without changing the old one.
  1064.  
  1065.           (setq needles-per-cluster
  1066.                 '((2 . ("Austrian Pine" "Red Pine"))
  1067.                   (3 . "Pitch Pine")
  1068.                   (5 . "White Pine")))
  1069.           =>
  1070.           ((2 "Austrian Pine" "Red Pine")
  1071.            (3 . "Pitch Pine")
  1072.            (5 . "White Pine"))
  1073.           
  1074.           (setq copy (copy-alist needles-per-cluster))
  1075.           =>
  1076.           ((2 "Austrian Pine" "Red Pine")
  1077.            (3 . "Pitch Pine")
  1078.            (5 . "White Pine"))
  1079.           
  1080.           (eq needles-per-cluster copy)
  1081.                => nil
  1082.           (equal needles-per-cluster copy)
  1083.                => t
  1084.           (eq (car needles-per-cluster) (car copy))
  1085.                => nil
  1086.           (cdr (car (cdr needles-per-cluster)))
  1087.                => "Pitch Pine"
  1088.           (eq (cdr (car (cdr needles-per-cluster)))
  1089.               (cdr (car (cdr copy))))
  1090.                => t
  1091.  
  1092.  
  1093. 
  1094. File: elisp,  Node: Sequences Arrays Vectors,  Next: Symbols,  Prev: Lists,  Up: Top
  1095.  
  1096. Sequences, Arrays, and Vectors
  1097. ******************************
  1098.  
  1099.    Recall that the "sequence" type is the union of three other Lisp
  1100. types: lists, vectors, and strings.  In other words, any list is a
  1101. sequence, any vector is a sequence, and any string is a sequence. 
  1102. The common property that all sequences have is that each is an
  1103. ordered collection of elements.
  1104.  
  1105.    An "array" is a single primitive object directly containing all
  1106. its elements.  Therefore, all the elements are accessible in constant
  1107. time.  The length of an existing array cannot be changed.  Both
  1108. strings and vectors are arrays.  A list is a sequence of elements,
  1109. but it is not a single primitive object; it is made of cons cells,
  1110. one cell per element.  Therefore, elements farther from the beginning
  1111. of the list take longer to access, but it is possible to add elements
  1112. to the list or remove elements.  The elements of vectors and lists
  1113. may be any Lisp objects.  The elements of strings are all characters.
  1114.  
  1115.    The following diagram shows the relationship between these types:
  1116.  
  1117.                  ___________________________________
  1118.                 |                                   |
  1119.                 |          Sequence                 |
  1120.                 |  ______   ______________________  |
  1121.                 | |      | |                      | |
  1122.                 | | List | |         Array        | |
  1123.                 | |      | |  ________   _______  | |   
  1124.                 | |______| | |        | |       | | |
  1125.                 |          | | String | | Vector| | |
  1126.                 |          | |________| |_______| | |
  1127.                 |          |______________________| |
  1128.                 |___________________________________|
  1129.  
  1130.            The Relationship between Sequences, Arrays, and Vectors
  1131.  
  1132. * Menu:
  1133.  
  1134. * Sequence Functions::    Functions that accept any kind of sequence.
  1135. * Arrays::                Characteristics of arrays in Emacs Lisp.
  1136. * Array Functions::       Functions specifically for arrays.
  1137. * Vectors::               Functions specifically for vectors.
  1138.  
  1139.  
  1140. 
  1141. File: elisp,  Node: Sequence Functions,  Next: Arrays,  Prev: Sequences Arrays Vectors,  Up: Sequences Arrays Vectors
  1142.  
  1143. Sequences
  1144. =========
  1145.  
  1146.    In Emacs Lisp, a "sequence" is either a list, a vector or a
  1147. string.  The common property that all sequences have is that each is
  1148. an ordered collection of elements.  This section describes functions
  1149. that accept any kind of sequence.
  1150.  
  1151.  * Function: sequencep OBJECT
  1152.      Returns `t' if OBJECT is a list, vector, or string, `nil'
  1153.      otherwise.
  1154.  
  1155.  * Function: copy-sequence SEQUENCE
  1156.      Returns a copy of SEQUENCE.  The copy is the same type of object
  1157.      as the original sequence, and it has the same elements in the
  1158.      same order.
  1159.  
  1160.      Storing a new element into the copy does not affect the original
  1161.      SEQUENCE, and vice versa.  However, the elements of the new
  1162.      sequence are not copies; they are identical (`eq') to the
  1163.      elements of the original.  Therefore, changes made within these
  1164.      elements, as found via the copied sequence, are also visible in
  1165.      the original sequence.
  1166.  
  1167.      See also `append' in *Note Building Lists::, `concat' in *Note
  1168.      Creating Strings::, and `vconcat' in *Note Vectors::, for others
  1169.      ways to copy sequences.
  1170.  
  1171.           (setq bar '(1 2))
  1172.                => (1 2)
  1173.           (setq x (vector 'foo bar))
  1174.                => [foo (1 2)]
  1175.           (setq y (copy-sequence x))
  1176.                => [foo (1 2)]
  1177.           
  1178.           (eq x y)
  1179.                => nil
  1180.           (equal x y)
  1181.                => t
  1182.           (eq (elt x 1) (elt y 1))
  1183.                => t
  1184.           
  1185.           ;; Replacing an element of one sequence.
  1186.           (aset x 0 'quux)
  1187.           x => [quux (1 2)]
  1188.           y => [foo (1 2)]
  1189.           
  1190.           ;; Modifying the inside of a shared element.
  1191.           (setcar (aref x 1) 69)
  1192.           x => [quux (69 2)]
  1193.           y => [foo (69 2)]
  1194.  
  1195.  * Function: length SEQUENCE
  1196.      Returns the number of elements in SEQUENCE.  If SEQUENCE is a
  1197.      cons cell that is not a list (because the final CDR is not
  1198.      `nil'), a `wrong-type-argument' error is signaled.
  1199.  
  1200.           (length '(1 2 3))
  1201.               => 3
  1202.           (length nil)
  1203.               => 0
  1204.           (length "foobar")
  1205.               => 6
  1206.           (length [1 2 3])
  1207.               => 3
  1208.  
  1209.  * Function: elt SEQUENCE INDEX
  1210.      This function returns the element of SEQUENCE indexed by INDEX. 
  1211.      Legitimate values of INDEX are integers ranging from 0 up to one
  1212.      less than the length of SEQUENCE; other values produce an
  1213.      `args-out-of-range' error.
  1214.  
  1215.           (elt [1 2 3 4] 2)
  1216.                => 3
  1217.           (elt '(1 2 3 4) 2)
  1218.                => 3
  1219.           (char-to-string (elt "1234" 2))
  1220.                => "3"
  1221.           (elt [1 2 3 4] 4)
  1222.                error-->Args out of range: [1 2 3 4], 4
  1223.           (elt [1 2 3 4] -1)
  1224.                error-->Args out of range: [1 2 3 4], -1
  1225.  
  1226.      This function duplicates `aref' (*note Array Functions::.) and
  1227.      `nth' (*note List Elements::.), except that it works for any
  1228.      kind of sequence.
  1229.  
  1230.  
  1231. 
  1232. File: elisp,  Node: Arrays,  Next: Array Functions,  Prev: Sequence Functions,  Up: Sequences Arrays Vectors
  1233.  
  1234. Arrays
  1235. ======
  1236.  
  1237.    An "array" object refers directly to a number of other Lisp
  1238. objects, called the elements of the array.  Any element of an array
  1239. may be accessed in constant time.  In contrast, an element of a list
  1240. requires access time that is proportional to the position of the
  1241. element in the list.
  1242.  
  1243.    When you create an array, you must specify how many elements it has.
  1244. The amount of space allocated depends on the number of elements. 
  1245. Therefore, it is impossible to change the size of an array once it is
  1246. created.  You cannot add or remove elements.  However, you can
  1247. replace an element with a different value.
  1248.  
  1249.    Emacs defines two types of array, both of which are
  1250. one-dimensional: "strings" and "vectors".  A vector is a general
  1251. array; its elements can be any Lisp objects.  A string is a
  1252. specialized array; its elements must be characters (i.e., integers
  1253. between 0 and 255).  Each type of array has its own read syntax. 
  1254. *Note String Type::, and *Note Vector Type::.
  1255.  
  1256.    Both kinds of arrays share these characteristics:
  1257.  
  1258.    * The first element of an array has index zero, the second element
  1259.      has index 1, and so on.  This is called "zero-origin" indexing. 
  1260.      For example, an array of four elements has indices 0, 1, 2, and 3.
  1261.  
  1262.    * The elements of an array may be referenced or changed with the
  1263.      functions `aref' and `aset', respectively (*note Array
  1264.      Functions::.).
  1265.  
  1266.    In principle, if you wish to have an array of characters, you
  1267. could use either a string or a vector.  In practice, we always choose
  1268. strings for such applications, for three reasons:
  1269.  
  1270.    * They occupy one-fourth the space of a vector of the same elements.
  1271.  
  1272.    * Strings are printed in a way that shows the contents more
  1273.      clearly as characters.
  1274.  
  1275.    * Many of the specialized editing and I/O facilities of Emacs
  1276.      accept only strings.  For example, you cannot insert a vector of
  1277.      characters into a buffer the way you can insert a string.  *Note
  1278.      Strings and Characters::.
  1279.  
  1280.  
  1281. 
  1282. File: elisp,  Node: Array Functions,  Next: Vectors,  Prev: Arrays,  Up: Sequences Arrays Vectors
  1283.  
  1284. Functions that Operate on Arrays
  1285. ================================
  1286.  
  1287.    In this section, we describe the functions that accept both
  1288. strings and vectors.
  1289.  
  1290.  * Function: arrayp OBJECT
  1291.      This function returns `t' if OBJECT is an array (i.e., either a
  1292.      vector or a string).
  1293.  
  1294.           (arrayp [a])
  1295.           => t
  1296.           (arrayp "asdf")
  1297.           => t
  1298.  
  1299.  * Function: aref ARRAY INDEX
  1300.      This function returns the INDEXth element of ARRAY.  The first
  1301.      element is at index zero.
  1302.  
  1303.           (setq primes [2 3 5 7 11 13])
  1304.                => [2 3 5 7 11 13]
  1305.           (aref primes 4)
  1306.                => 11
  1307.           (elt primes 4)
  1308.                => 11
  1309.           
  1310.           (aref "abcdefg" 1)
  1311.                => 98           ; `b' is ASCII code 98.
  1312.  
  1313.       See also the function `elt', in *Note Sequence Functions::.
  1314.  
  1315.  * Function: aset ARRAY INDEX OBJECT
  1316.      This function sets the INDEXth element of ARRAY to be OBJECT. 
  1317.      It returns OBJECT.
  1318.  
  1319.           (setq w [foo bar baz])
  1320.                => [foo bar baz]
  1321.           (aset w 0 'fu)
  1322.                => fu
  1323.           w
  1324.                => [fu bar baz]
  1325.           
  1326.           (setq x "asdfasfd")
  1327.                => "asdfasfd"
  1328.           (aset x 3 ?Z)
  1329.                => 90
  1330.           x
  1331.                => "asdZasfd"
  1332.  
  1333.      If ARRAY is a string and OBJECT is not a character, a
  1334.      `wrong-type-argument' error results.
  1335.  
  1336.  * Function: fillarray ARRAY OBJECT
  1337.      This function fills the array ARRAY with pointers to OBJECT,
  1338.      replacing any previous values.  It returns ARRAY.
  1339.  
  1340.           (setq a [a b c d e f g])
  1341.                => [a b c d e f g]
  1342.           (fillarray a 0)
  1343.                => [0 0 0 0 0 0 0]
  1344.           a
  1345.                => [0 0 0 0 0 0 0]
  1346.           (setq s "When in the course")
  1347.                => "When in the course"
  1348.           (fillarray s ?-)
  1349.                => "------------------"
  1350.  
  1351.      If ARRAY is a string and OBJECT is not a character, a
  1352.      `wrong-type-argument' error results.
  1353.  
  1354.    The general sequence functions `copy-sequence' and `length' are
  1355. often useful for objects known to be arrays.  *Note Sequence
  1356. Functions::.
  1357.  
  1358.  
  1359.